package com.qianguo.shop.utils;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by eva on 15/11/17.
 *
 * 加密模块
 *
 */
public class Cryptor {

    /**
     * 对密码字段进行加密，加密算法是先将明文进行一轮MD5加密，然后将第一轮加密得到的密文反转，
     * 并在尾部添加附加的字段，然后再进行一轮MD5加密
     *
     * @param words 密码
     * @param key 附加的字段
     * @param encoding 编码
     * @return 密文
     * @throws UnsupportedEncodingException
     */
    public static String encryptPassword(char[] words, String key, String encoding)
            throws UnsupportedEncodingException {
        final int length = words.length;
        Charset charset = Charset.forName(encoding);
        CharBuffer charBuffer = CharBuffer.allocate(length);
        charBuffer.put(words).flip();
        ByteBuffer byteBuffer = charset.encode(charBuffer);
        byte[] rawBytes = byteBuffer.array();
        try {
            // 进行第一轮MD5加密
            String encrypted = encryptByMD5(rawBytes);
            // 将第一轮加密得到的结果进行反转，然后在尾端添加key
            encrypted = new StringBuffer(encrypted).reverse().append(key).toString();
            // 再进行一轮MD5加密
            encrypted = encryptByMD5(encrypted.getBytes(encoding));
            return encrypted;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 使用UTF8编码对明文进行MD5加密
     *
     * @param content 明文内容
     * @return 密文
     * @throws UnsupportedEncodingException
     * @throws NoSuchAlgorithmException
     */
    public static String encryptByMD5(String content)
            throws UnsupportedEncodingException, NoSuchAlgorithmException {
        return encryptByMD5(content, "utf-8");
    }

    /**
     * 根据指定的编码对明文进行MD5加密
     *
     * @param content 明文
     * @param charsetName 编码名称
     * @return 密文
     * @throws UnsupportedEncodingException
     * @throws NoSuchAlgorithmException
     */
    public static String encryptByMD5(String content, String charsetName)
            throws UnsupportedEncodingException, NoSuchAlgorithmException {
        return encryptByMD5(content.getBytes(charsetName));
    }

    /**
     * 对明文的字节流进行MD5加密
     *
     * @param content 明文字节流
     * @return 密文
     * @throws NoSuchAlgorithmException
     */
    public static String encryptByMD5(byte[] content) throws NoSuchAlgorithmException {
        return _encryptByMD5(content);
    }

    private static String _encryptByMD5(byte[] content) throws NoSuchAlgorithmException {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] encryptedBytes = md5.digest(content);
        StringBuffer sb = new StringBuffer();
        final int encryptLength = encryptedBytes.length;
        for (int i = 0; i < encryptLength; i++) {
            int value = ((int) encryptedBytes[i]) & 0xff;
            if (value < 0x10) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(value));
        }
        return sb.toString();
    }
}
